﻿using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class CMRepository:ICMRepository
    {
        private readonly MyDbContext myDbContext;

        public CMRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }

        public async Task<List<CM>> GetCMAsync()
        {
            return await myDbContext.CM.ToListAsync();
        }
        //删除
       public async Task<bool> DeleteCMAsync(int id)
        {
            CM cm = await myDbContext.CM.FindAsync(id);
            if (cm == null)
            {
                return false;
            }

            myDbContext.CM.Remove(cm);
            int changes = await myDbContext.SaveChangesAsync();
            return changes > 0;
        }
        //新增
        public async Task<bool> AddCMAsync(CM cM)
        {
            myDbContext.CM.Add(cM);
            return await myDbContext.SaveChangesAsync() > 0;
        }

		public async Task<List<CM>> GetCMAsyncById(string id)
		{
			return await myDbContext.CM.Where(e => e.MajorKindId == id).ToListAsync();
		}

		public async Task<CM> GetCMSAsyncById(string id)
		{
			return await myDbContext.CM.Where(e => e.MajorId == id).FirstOrDefaultAsync();
		}
	}
}
